package com.eqdd.yiqidian.kotlin

import com.eqdd.yiqidian.bean.rebuild.OptionBean
import com.eqdd.yiqidian.http.HttpCallBack
import com.eqdd.yiqidian.http.HttpConfig
import com.eqdd.yiqidian.utils.ToastUtil
import com.eqdd.yiqidian.widget.ActionSheetDialog
import com.eqdd.yiqidian.widget.ChooseAlertView
import com.lzy.okgo.OkGo
import com.lzy.okgo.cache.CacheMode
import com.lzy.okgo.model.Response
import java.util.ArrayList

/**
 * @author吕志豪 .
 * @date 18-3-24  上午9:58.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */
object SingleChooseUtil {

    fun getChooseDialog(activity: KotlinBaseActivity, option: Int, onGetDialog: OnGetDialog, onSingChooseListener: OnSingChooseListener) {
        OkGo.get<List<OptionBean>>(HttpConfig.BASE_URL + HttpConfig.AREA_OPTIONS)
                .params("type", option)
                .cacheMode(CacheMode.IF_NONE_CACHE_REQUEST)
                .cacheKey(HttpConfig.BASE_URL + HttpConfig.AREA_OPTIONS + option)
                .execute(object : HttpCallBack<List<OptionBean>>(activity) {
                    override fun onSuccess(response: Response<List<OptionBean>>) {
                        val optionBean = response.body()
                        val strings = ArrayList<String>()
                        for (optionsBean in optionBean) {
                            strings.add(optionsBean.name)
                        }
                        val actionSheetDialog = ChooseAlertView.getBottomChoose(activity, "请选择", strings) { v, p ->
                            onSingChooseListener.onSingChooseListener(p, strings)

                        }
                        onGetDialog.onGetDialog(actionSheetDialog)
                    }

                    override fun onCacheSuccess(response: Response<List<OptionBean>>?) {
                        super.onCacheSuccess(response)
                        val optionBean = response!!.body()
                        val strings = ArrayList<String>()
                        for (optionsBean in optionBean) {
                            strings.add(optionsBean.name)
                        }
                        val actionSheetDialog = ChooseAlertView.getBottomChoose(activity, "请选择", strings) { v, p ->
                            onSingChooseListener.onSingChooseListener(p, strings)

                        }
                        onGetDialog.onGetDialog(actionSheetDialog)
                    }

                    override fun onError(response: Response<List<OptionBean>>) {
                        super.onError(response)
                        ToastUtil.showShort("数据获取失败，请稍候重试")
                    }
                })
    }

    interface OnSingChooseListener {
        fun onSingChooseListener(position: Int, strings: List<String>)
    }

    interface OnGetDialog {
        fun onGetDialog(sheetDialog: ActionSheetDialog)
    }


}